package x.ovo.wechat.bot.http.request.group;

import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.json.JSONUtil;
import x.ovo.wechat.bot.common.Constant;
import x.ovo.wechat.bot.common.entity.Contact;
import x.ovo.wechat.bot.common.http.request.ApiRequest;
import x.ovo.wechat.bot.common.http.session.LoginSession;

import java.util.Map;
import java.util.function.Function;

/**
 * 修改组名请求
 *
 * @author ovo on 2024/05/06.
 * @since 1.0.0
 */
public class ModifyGroupNameRequest extends ApiRequest<Boolean> {

    private final String newGroupName;
    private final Contact group;

    public ModifyGroupNameRequest(LoginSession session, String newGroupName, Contact group) {
        super(session);
        this.newGroupName = newGroupName;
        this.group = group;
    }

    @Override
    public String getUrl() {
        return UrlBuilder.of(this.session.getUrl())
                .addPath(Constant.UPDATE_GROUP)
                .addQuery("fun", "modtopic")
                .build();
    }

    @Override
    public Map<String, Object> getParameters() {
        super.addParameter("NewTopic", newGroupName)
                .addParameter("ChatRoomName", group.getUserName());
        return super.getParameters();
    }

    @Override
    public String getMethod() {
        return POST;
    }

    @Override
    public boolean isJson() {
        return true;
    }

    @Override
    public Function<String, Boolean> responseHandler() {
        return s -> JSONUtil.parseObj(s).getJSONObject("BaseResponse").getInt("Ret") == 0;
    }
}
